//
//  HMEmoticonPageViewCell.swift
//  CZWeibo31
//
//  Created by apple on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMEmoticonPageViewCell: UICollectionViewCell {
    
    // 定义一个属性(一维数组 8  20) 供外界调用
    var emoticons:[HMEmoticonModel]?{
        didSet{
            
            // 先全部隐藏(cell 复用问题)
            for button in buttonsArray {
                button.hidden = true
            }
            
            // 遍历emoticons 
            for (i,emoticonModel) in emoticons!.enumerate() {
                
                // 获取button
                let button = buttonsArray[i]
                // 给butto的 模型赋值
                button.emoticonModel = emoticonModel
                // 显示button(cell 复用问题)
                button.hidden = false
                // 判断他是图片表情 还是emoji表情
                // 是emoji 表情 需要设置button 的title
                if emoticonModel.isEmoji {
                    let code = (emoticonModel.code ?? "" as NSString).emoji()
                    button.setTitle(code, forState: UIControlState.Normal)
                    // (cell 复用问题)
                    button.setImage(nil, forState: UIControlState.Normal)
                    
                }else {
                    // 是图片表情 需要设置button 的image
//                    let png = emoticonModel.png ?? ""
//                    let path = emoticonModel.path ?? ""
//                    let image = UIImage(named: "\(path)\(png)", inBundle: HMEmoticonTools.sharedTools.emoticonsBundle, compatibleWithTraitCollection: nil)
                    button.setImage(UIImage.emoticonsImage(emoticonModel), forState: UIControlState.Normal)
                    // (cell 复用问题)
                    button.setTitle(nil, forState: UIControlState.Normal)
                }
            }
            
        }
    }
    
    var indexPath: NSIndexPath?{
        didSet{
            // 设置text
            //messageLabel.text = "第\(indexPath?.section ?? 0)组\n第\(indexPath?.item ?? 0)页"
        }
    }
    
    // 定义一个可变的空数组
    var buttonsArray:[HMEmoticonButton] = [HMEmoticonButton]()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 监听方法
    func deleteButtonClick(){
        print("删除按钮店家")
        // 发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBEMOTICONDELETEBUTTONNOTIFICATION, object: nil)
    }
    // 20个按钮点击
    func buttonClick(button: HMEmoticonButton) {
        // 发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBEMOTICONBUTTONNOTIFICATION, object: button.emoticonModel)
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        // 添加控件
        addChildButtons()
        contentView.addSubview(deleteButton)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 计算button的宽和高
        let w = (self.frame.width - 10)/CGFloat(HMEmoticonsMaxCol)
        let h = (self.frame.height - 20)/CGFloat(HMEmoticonsMaxRow)
        // 设置20个按钮的frame
        // 遍历buttonsArray
        for (index, button) in buttonsArray.enumerate() {
            // 设置frame
            let col = CGFloat(index%HMEmoticonsMaxCol)
            let row = CGFloat(index/HMEmoticonsMaxCol)
            button.frame = CGRect(x: col*w + 5, y: row*h, width: w, height: h)
        }
        
        // 给删除按钮设置frame
        deleteButton.frame = CGRect(x: self.frame.width - 5 - w, y: h*2, width: w, height: h)
        
    }
    
    // 创建20按钮
    func addChildButtons(){
        // 循环创建button
        for _ in 0..<HMEmoticonsMaxCount {
            let button = HMEmoticonButton()
            // 添加点击事件
            button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            // 设置字体大小
            button.titleLabel?.font = UIFont.systemFontOfSize(30)
            // 添加button
            buttonsArray.append(button)
            // 添加button
            contentView.addSubview(button)
        }
    }
    
    // MARK: - 懒加载控件
    private lazy var messageLabel: UILabel = UILabel(textColor: UIColor.redColor(), textSize: 35, maxWidth: SCREENW)
    // 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        // 添加点击事件
        button.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()
}






